<?php


namespace app\admin\controller\mall;

use app\admin\model\MallExpress;
use app\admin\model\MallGoods;
use app\admin\model\MallOrder;
use app\api\help\SendMsg;
use app\api\model\AllInPay;
use app\api\model\MallCollage;
use app\api\model\MallTradeRecordLog;
use app\api\model\MallUser;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="拼团中订单")
 */
class OrderStatus10 extends AdminController
{
    
    
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\MallOrder();
        
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function statusIndex()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[] = ['order_status', '=', 10];
            $where[] = ['ea_mall_order.is_virtual', '=', 0];
            $count   = $this->model
                ->with(['goods', 'user', 'collage'])
                ->where($where)
                ->count();
            $list    = $this->model
                ->with(['goods', 'user', 'collage'])
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data    = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    
    
    /**
     * @NodeAnotation(title="订单详情")
     */
    public function detail($id)
    {
        $row = $this->model->with(['goods', 'user'])->find($id)->toArray();
        empty($row) && $this->error('数据不存在');
        switch ($row[ 'order_status' ]) {
            case 0:
                $row[ 'order_status' ] = '未支付';
                break;
            case 1:
                $row[ 'order_status' ] = '拼团已结束';
                break;
            case 10:
                $row[ 'order_status' ] = '进行中';
                break;
            case 20:
                $row[ 'order_status' ] = '取消';
                break;
            case 30:
                $row[ 'order_status' ] = '拼团成功';
                break;
            case 40:
                $row[ 'order_status' ] = '拼团失败';
                break;
            case 50:
                $row[ 'order_status' ] = '已发货';
                break;
            case 60:
                $row[ 'order_status' ] = '已完成';
                break;
            case 70:
                $row[ 'order_status' ] = '已退款';
                break;
        }
        $row[ 'collage_num' ] = MallCollage::where([['collage_id', '=', $row[ 'collage_id' ]]])->value('collage_num') ?? 0;
        $row[ 'order_num' ]   = MallOrder::where([['u_collage_id', '=', $row[ 'collage_id' ]], ['order_status', 'in', '10,30,50,60']])->count() ?? 0;
        $row[ 'order_user' ]  = [];
        if ($row[ 'order_num' ] > 0) {
            $userIds             = MallOrder::where([['u_collage_id', '=', $row[ 'collage_id' ]], ['order_status', 'in', '10,30,50,60']])->column('user_id');
            $row[ 'order_user' ] = MallUser::where([['id', 'in', $userIds]])->select()->toArray();
        }
        $row[ 'user_order_num' ]  = MallOrder::where([['u_collage_id', '=', $row[ 'collage_id' ]], ['order_status', 'in', '10,30,50,60'], ['uid', '=', $row[ 'user_id' ]]])->count();
        $row[ 'user_order_user' ] = [];
        if ($row[ 'user_order_num' ] > 0) {
            $userIds                  = MallOrder::where([['u_collage_id', '=', $row[ 'collage_id' ]], ['order_status', 'in', '10,30,50,60'], ['uid', '=', $row[ 'user_id' ]]])->column('user_id');
            $row[ 'user_order_user' ] = MallUser::where([['id', 'in', $userIds]])->select()->toArray();
        }
        $row[ 'total_price' ]     = getPrice($row[ 'total_price' ]);
        $row[ 'order_price' ]     = getPrice($row[ 'order_price' ]);
        $row[ 'pay_price' ]       = getPrice($row[ 'pay_price' ]);
        $row[ 'pay_balance' ]     = getPrice($row[ 'pay_balance' ]);
        $row[ 'failure_subsidy' ] = getPrice($row[ 'failure_subsidy' ]);
        $this->assign('row', $row) ?? 0;
        return $this->fetch();
    }
    
    
    /**
     * @NodeAnotation(title="允许退款")
     */
    public function permitRefund($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->find();
        if ( !in_array($row->order_status, [10, 30, 60]) || $row->refund_status != 0) {
            $this->error('订单状态异常，请刷新重试');
        }
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->save(['refund_status' => 1, 'order_status' => 70, 'collage_status' => 4]);
            MallCollage::where('collage_id', $row->collage_id)->update(['status' => 0]);
        } catch (\Exception $e) {
            $this->error('允许退款失败');
        }
        $save ? $this->success('允许退款成功') : $this->error('允许退款失败');
    }
}